923C - Perfect Security - CodeForces Solution


data structures greedy strings trees *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  const long long mxn = 32 * 3e5;
  vector<vector<long long>*> tr(mxn);
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<long long> cnt(mxn);
  long long tot = 1;
  auto ins = [&](long long x) {
    long long p = 1;
    ++cnt[p];
    for (long long i = 30; i >= 0; --i) {
      long long b = (x >> i) & 1;
      if (!tr[p]) {
        tr[p] = new vector<long long>(2);
      }
      if (!(*tr[p])[b]) {
        (*tr[p])[b] = ++tot;
      }
      p = (*tr[p])[b];
      ++cnt[p];
    }
    ++cnt[p];
  };
  auto qry = [&](long long x) {
    long long p = 1;
    --cnt[p];
    long long res = 0;
    for (long long i = 30; i >= 0; --i) {
      long long b = (x >> i) & 1;
      if (!tr[p] || cnt[(*tr[p])[b]] <= 0) {
        p = (*tr[p])[!b];
        res += (1 << i);
      } else {
        p = (*tr[p])[b];
      }
      --cnt[p];
    }
    --cnt[p];
    return res;
  };
  for (long long i = 0; i < n; ++i) {
    long long b;
    cin >> b;
    ins(b);
  }
  for (long long i = 0; i < n; ++i) {
    cout << qry(a[i]) << ' ';
  }
  cout << '\n';
  return 0;
}
// CAxLUsqDnJpPQXAQrstkugFNFnVqwRmkpcjdPllHQrSPolOgdBlLCaaBEvtjmwnlRGgdiEHKoqJpbdSdPgDBBnzlGoPPSONjBSwzlqLlsiERiHgHgrEqwwpNgbiGfXoC


Comments

Submit
0 Comments
More Questions

1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even